import 'package:development_assistant/config/colors.dart';
import 'package:development_assistant/controller/test_controller.dart';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../../config/font.dart';
import '../../../../model/ChartData.dart';
import '../../widget/custom_text.dart';

class TestResult extends StatelessWidget {

  final TestController testController=Get.put(TestController());
  TestResult({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:_appBar(),
      bottomNavigationBar: _saveResult(),
      body: FutureBuilder(
        future: testController.test(),
        builder: (context,snapshot) {
          return SingleChildScrollView(
            padding: EdgeInsets.symmetric(horizontal: 20.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("测试数据".tr,style: FontConfig.titleStyle,),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 20.w,vertical: 10.h),
                  margin: EdgeInsets.only(bottom: 10.h),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(15),
                      border: Border.all(color:Colors.grey,width: 1)
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          _charts(),
                          _testData(),
                        ],
                      ),
                      CustomText(text: "${'测试环境'.tr} :  ${testController.testEnv.toString().tr} ", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 12.sp)),
                      SizedBox(height: 3.h,),
                      CustomText(text: "${'测试线程'.tr} :  ${testController.testThread}", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 12.sp)),
                      SizedBox(height: 3.h,),
                      CustomText(text: "${'循环次数'.tr} : ${testController.testCycles}", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 12.sp)),
                      SizedBox(height: 3.h,),
                      CustomText(text: "${'间隔时长'.tr} : ${testController.testInterval} ms", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 12.sp)),
                      SizedBox(height: 3.h,),
                      CustomText(text: "${'测试总耗时'.tr} : ${testController.requestTime} ms", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 12.sp)),
                      SizedBox(height: 3.h,),
                      CustomText(text: "${'平均接口请求耗时'.tr} : ${testController.averageTime} ms", textStyle: TextStyle(fontWeight: FontWeight.bold,fontSize: 12.sp)),
                    ],
                  ),
                ),
                Text("测试步骤".tr,style: FontConfig.titleStyle,),
                _testApi()
              ],
            ),
          );
        }
      ),
    );
  }

  _charts(){
    var seriesList = [
      charts.Series(
        id: 'Pie',
        domainFn: (ChartData data, _) => data.pieLabel,
        measureFn: (ChartData data, _) => data.pieValue,
        data: [
          ChartData('success', testController.success.value),
          ChartData('error', testController.error.value),
        ],
      ),
    ];
    return SizedBox(
        width: 130.w,
        height: 130.w,
        child: charts.PieChart(
          seriesList,
          animate: true,
        )
    );
  }

  _testData(){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Container(
              width: 10.w,
              height: 10.w,
              margin: EdgeInsets.symmetric(horizontal: 10.w),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(50),
                  color: Colors.green
              ),
            ),
            Text("${'成功次数'.tr} : ${testController.success}",style: FontConfig.fieldTitle,),
          ],
        ),
        Row(
          children: [
            Container(
              width: 10.w,
              height: 10.w,
              margin: EdgeInsets.symmetric(horizontal: 10.w),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(50),
                  color: Colors.green
              ),
            ),
            Text("${'失败次数'.tr} : ${testController.error}",style: FontConfig.fieldTitle,),
          ],
        )
      ],
    );
  }

  _testApi(){
    return Column(
      children: [
        //参数列表
        Obx(() => Wrap(
          children: List<Widget>.generate(testController.testApi.length, (index) {
            return Container(
              height: 70.h,
              margin: EdgeInsets.only(bottom: 10.h),
              padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 15.h),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  border: Border.all(color:Colors.grey,width: 1)
              ),
              child: Row(
                children: [
                  Container(
                    width: 45.w,
                    height: 45.w,
                    margin: EdgeInsets.symmetric(horizontal: 5.w),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(50),
                        color: ColorsConfig.methodTagColors[testController.testApi[index].apiMethod]
                    ),
                    child: Center(child: Text(testController.testApi[index].apiMethod,style: TextStyle(color: Colors.white),)),
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      CustomText(text: testController.testApi[index].apiName, textStyle: FontConfig.apiName),
                      SizedBox(height: 5,),
                      SizedBox(width: 240.w,child: CustomText(text: testController.testApi[index].apiUrl, textStyle: FontConfig.urlStyle))
                    ],
                  )
                ],
              ),
            );
          }),
        ))
      ],
    );
  }

  _saveResult(){
    return Container(
      width: 200.w,
      height: 35.h,
      margin: EdgeInsets.symmetric(vertical: 10.h),
      padding: EdgeInsets.symmetric(horizontal: 100.w),
      child: ElevatedButton(
        onPressed: (){
          Get.defaultDialog(
            title: "保存测试数据".tr,
            middleText: "是否保存该测试数据?".tr,
            confirm: TextButton(
              style: TextButton.styleFrom(
                  backgroundColor: ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
              onPressed: (){
                testController.saveResult();
                Get.offAllNamed("/Home",arguments: {"selectIndex":2});
              },
              child: Text("确定".tr),
            ),
            cancel:TextButton(
              style: TextButton.styleFrom(
                  backgroundColor: ColorsConfig().buttonColorOfTheme,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
              onPressed: (){
                Get.back();
              },
              child:Text("取消".tr),
            ),
          );
        },
        style: ElevatedButton.styleFrom(
            backgroundColor: ColorsConfig().buttonColorOfTheme,
            foregroundColor: Colors.white,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20)
            )
        ),
        child: Text("保存结果".tr,style: TextStyle(fontWeight: FontWeight.bold),),
      ),
    );
  }

  _appBar(){
    return AppBar(
      title:Text("测试结果".tr),
      centerTitle: true,
      leading: IconButton(
        onPressed: (){

          Get.offNamed("/AutoTest");
        },
        icon: const Icon(Icons.arrow_back),
      ),
    );
  }

}
